languageMapping.ts ➔ determineLanguage   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
export type WordlistLanguage =
2
  | 'english'
3
  | 'spanish'
4
  | 'french'
5
  | 'czech'
6
  | 'italian'
7
  | 'portuguese'
8
  | 'japanese'
9
  | 'korean'
10
  | 'chinese_simplified'
11
  | 'chinese_traditional';
12
13
export type UILanguage = 'en' | 'es' | 'fr' | 'cs' | 'it' | 'pt' | 'ja' | 'ko' | 'zh-Hans' | 'zh-Hant';
14
15
export const DEFAULT_LANGUAGE: WordlistLanguage = 'english';
16
export const DEFAULT_UI_LANGUAGE: UILanguage = 'en';
17
18
/**
19
 * Mapping from wordlist language to UI language code
20
 */
21
const wordlistToUIMap: Record<WordlistLanguage, UILanguage> = {
22
  english: 'en',
23
  spanish: 'es',
24
  french: 'fr',
25
  czech: 'cs',
26
  italian: 'it',
27
  portuguese: 'pt',
28
  japanese: 'ja',
29
  korean: 'ko',
30
  chinese_simplified: 'zh-Hans',
31
  chinese_traditional: 'zh-Hant',
32
};
33
34
export function wordlistToUILanguage(wordlistLang: string): UILanguage {
35
  return wordlistToUIMap[wordlistLang as WordlistLanguage] || DEFAULT_UI_LANGUAGE;
36
}
37
38
export function getDefaultLanguage(): WordlistLanguage {
39
  return DEFAULT_LANGUAGE;
40
}
41
42
export function determineLanguage(savedLanguage: string | null): WordlistLanguage {
43
  if (savedLanguage && isValidWordlistLanguage(savedLanguage)) {
44
    return savedLanguage as WordlistLanguage;
45
  }
46
  return DEFAULT_LANGUAGE;
47
}
48
49
export function isValidWordlistLanguage(lang: string): boolean {
50
  return Object.keys(wordlistToUIMap).includes(lang);
51
}
52
53
export function languagesMatch(lang1: string, lang2: string): boolean {
54
  return lang1 === lang2;
55
}
56
57
export function getSupportedLanguages(): WordlistLanguage[] {
58
  return Object.keys(wordlistToUIMap) as WordlistLanguage[];
59
}
60